#!/bin/bash
# checkouts your google code project and makes a zip and uploads it to the downloads section
#  -s SUMMARY, Short description of the file
#  -p PROJECT, Google Code project name
#  -u USER, Your Google Code username
#  -w PASSWORD, Your Google Code password
#  -l LABELS, An optional list of comma-separated labels to attach to the file
#  -f FILENAME, the file name to be used on the upload

SCRIPT_USAGE="\
\n    Usage: $0
\n
\n    Required parameters
\n
\n      -p Google Code project name
\n
\n    Optional parameters
\n
\n      -u Your Google Code username (will ask if not provided)
\n      -w Your Google Code password (will ask if not provided)
\n      -s Short description of the file (Defaults to project name)
\n      -l A list of comma-separated labels to attach to the file
\n      -f the name of the file to upload (Defaults to project name)
\n
\n"


# Iterate over the options passed to the script
while getopts "s:p:u:w:l:f:" MYOPTION;
do
	case "$MYOPTION" in
		s)	    GOOGLE_CODE_SUMMARY="$OPTARG" ;;
		p) 	    GOOGLE_CODE_PROJECT="$OPTARG" ;;
		u)      GOOGLE_CODE_USER="$OPTARG" ;;
		w)      GOOGLE_CODE_PASSWORD="$OPTARG" ;;
		l)      GOOGLE_CODE_LABELS="$OPTARG" ;;
		f)      GOOGLE_CODE_FILENAME="$OPTARG" ;;
	esac
done


# if all the required options are there
if [ -z "${GOOGLE_CODE_PROJECT}" ];
then
    # One of the required arguments is missing
    echo -e $SCRIPT_USAGE
else
     # setup default values if necessary
    GOOGLE_CODE_FILENAME=${GOOGLE_CODE_FILENAME:="$GOOGLE_CODE_PROJECT"}
    GOOGLE_CODE_SUMMARY=${GOOGLE_CODE_SUMMARY:="latest $GOOGLE_CODE_PROJECT"}
    # fetch project from svn
    echo "fetching $GOOGLE_CODE_PROJECT from google svn repository"
    svn export -q http://$GOOGLE_CODE_PROJECT.googlecode.com/svn/trunk/ $GOOGLE_CODE_PROJECT

    # zip the project
    echo "zipping $GOOGLE_CODE_PROJECT to $GOOGLE_CODE_FILENAME.zip"
    zip -r -q $GOOGLE_CODE_FILENAME.zip $GOOGLE_CODE_PROJECT

    # remove the svn download folders
    echo "removing svn downloaded files"
    rm -rf $GOOGLE_CODE_PROJECT

    # upload the project to the google code downloads
    echo "uploading $GOOGLE_CODE_PROJECT to google code downloads area"
    ./googlecode_upload.py -s "$GOOGLE_CODE_SUMMARY" -p "$GOOGLE_CODE_PROJECT" -u "$GOOGLE_CODE_USER" -w "$GOOGLE_CODE_PASSWORD" -l "$GOOGLE_CODE_LABELS" $GOOGLE_CODE_FILENAME.ZIP

    # remove the zip file
    echo "Removing $GOOGLE_CODE_FILENAME.zip after uploaded to google"
    rm -rf $GOOGLE_CODE_FILENAME.ZIP   
fi

